package com.v.im.webservice.service.impl;

import com.v.im.businessapi.dto.paramDTO.ImUserParamDTO;
import com.v.im.businessapi.dto.resultDTO.ImChatGroupUserResultDTO;
import com.v.im.businessapi.dto.resultDTO.ImUserResultDTO;
import com.v.im.commonmodule.utils.chat.RegisterResult;
import com.v.im.webservice.dto.resultDTO.UserInitResultDTO;
import com.v.im.webservice.service.base.BaseService;
import com.v.im.webservice.service.iface.IImUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wym
 * @create 2019-03-13 9:48
 **/
@Service
public class ImUserServiceImpl extends BaseService implements IImUserService {


    @Override
    public UserInitResultDTO getUserInitInfo(String userName,HttpServletRequest request) {
        UserInitResultDTO resultDTO = new UserInitResultDTO();

        //获取好友信息
        ImUserResultDTO userResultDTO = imUserClientService.getImUserByUserName(userName);
        resultDTO.setFriends(imUserFriendClientService.getUserFriends(userResultDTO.getId()));
        //获取本人信息
        String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        userResultDTO.setAvatar(host + userResultDTO.getAvatar());
        userResultDTO.setPassword(null);
        resultDTO.setMe(userResultDTO);

        //用户的群组信息
        resultDTO.setGroups(imChatGroupClientService.getUserGroups(userResultDTO.getId()));
        return resultDTO;
    }

    @Override
    public RegisterResult registerUserInfo(String name, String password, String phone) {
        RegisterResult registerResult = new RegisterResult();
        ImUserResultDTO imUserResultDTO = imUserClientService.getImUserByUserName(phone);
        if (imUserResultDTO != null && StringUtils.isNotBlank(imUserResultDTO.getId())) {
            registerResult.setResultCode(RegisterResult.ERROR);
            registerResult.setMessage("手机号码重复");
        } else {
            try {
                ImUserParamDTO imUser = new ImUserParamDTO();
                String finalSecret = "{bcrypt}" + new BCryptPasswordEncoder().encode(password.trim());
                imUser.setPassword(finalSecret);
                imUser.setLoginName(phone);
                imUser.setMobile(phone);
                imUser.setName(name);
                imUser.setAvatar("/img/default-user.png");
                imUserClientService.saveUser(imUser);
                registerResult.setResultCode(RegisterResult.SUCCESS);
                registerResult.setMessage("SUCCESS");
            } catch (Exception e) {
                registerResult.setResultCode(RegisterResult.ERROR);
                registerResult.setMessage("保存用户失败");
            }
        }
        return registerResult;
    }

    @Override
    public List<ImUserResultDTO> getChatUserList(String chatId) {
        List<ImUserResultDTO> resultDTOS = new ArrayList<>();
        List<ImChatGroupUserResultDTO> imChatGroupUserResultDTOS = imChatGroupUserClientService.getImChatGroupResults(chatId);
        if (CollectionUtils.isNotEmpty(imChatGroupUserResultDTOS)) {
            resultDTOS = imUserClientService.getImUserResults(imChatGroupUserResultDTOS.stream().map(ImChatGroupUserResultDTO::getUserId).collect(Collectors.joining(",")));
        }
        return resultDTOS;
    }
}
